<?php
/**
 * Create By: PhpStorm
 * User: niechenguang
 * File: GetCircle.php
 * Date: 2017/1/2
 * Time: 23:13
 */

namespace app\api\model\page\circle;

use app\api\model\dao\Circle;
use app\api\model\dao\ContributionRank;
use app\api\model\dao\DashangRecord;
use app\api\model\dao\Note;
use app\api\model\dao\NotePraise;
use app\api\model\dao\NoteReply;
use app\home\model\UserAccount;
use app\library\Error;
use app\library\PageBase;
use think\Exception;
use think\Validate;

class GetCircleContribution extends PageBase
{

    private $objCircle;
    private $objContributionRank;
    private $objUserAccount;
    private $objNote;
    private $objNotePraise;
    private $objNoteReply;
    private $objDashang;

    public function __construct()
    {
        parent::__construct();

        $this->objCircle = new Circle();
        $this->objUserAccount = new \app\api\model\dao\UserAccount();
        $this->objContributionRank = new ContributionRank();
        $this->objNote = new Note();
        $this->objNotePraise = new NotePraise();
        $this->objNoteReply = new NoteReply();
        $this->objDashang = new DashangRecord();

    }

    /**
     * @param $param
     * @return array|bool
     * @DESC 验证接口字段
     */
    public function checkParam($param)
    {
        $rule = [
            'cir_id' => 'require',
        ];
        $msg = [
            'cir_id.require' => '参数缺少圈子ID',
        ];

        $validate = new Validate($rule, $msg);
        $result = $validate->check($param);
        if (!$result) {
            return $validate->getError();
        }
        return '';
    }

    /**
     * page 入口
     *
     * @param $param
     * @return array
     * @throws Exception
     */
    public function myExcute($param)
    {

        // 1、参数校验
        $ret = $this->checkParam($param);
        if ($ret) {
            throw new Exception($ret, Error::ERR_PARAM);
        }

        // 1、获取贡献列表
        $contribution_rank = $this->objContributionRank->getRank($param['cir_id']);
        if (false === $contribution_rank) {
            throw new Exception('get circle contribution list fail. ', Error::ERR_SYS);
        }

        $userid = [];
        foreach ($contribution_rank as $val) {
            $userid[] = $val['userid'];
        }

        $userid2User = [];
        $userid2NoteCnt = [];
        if (!empty($userid)) {
            $rank_user_account = $this->objUserAccount->getUserAccountByUserids($userid);
            if (!empty($rank_user_account)) {
                foreach ($rank_user_account as $val) {
                    $userid2User[$val['id']] = $val;
                }
            }
            $note_cnt_ret = $this->objNote->getNoteCountByUserids($userid);
            if (!empty($note_cnt_ret)) {
                foreach ($note_cnt_ret as $val) {
                    $userid2NoteCnt[$val['creator']] = $val;
                }
            }
        }

        $i = 1;
        foreach ($contribution_rank as $val) {
            if (empty($userid2User[$val['userid']])) {
                continue;
            }
            $conds = [
                'note_userid' => $val['userid'],
                'is_delete' => 0,
            ];
            $praise_cnt = $this->objNotePraise->getNotePraiseCountByConds($conds);
            empty($praise_cnt) && $praise_cnt = 0;

            $note_reply_cnt = $this->objNoteReply->getNoteReplyCountByUserid($val['userid']);
            empty($note_reply_cnt) && $note_reply_cnt = 0;

            $dashang_money = 0;
            $conds = [
                'note_userid' => ['=', $val['userid']],
            ];
            $dashang = $this->objDashang->getDashangRecord($conds, ['money']);
            if (!empty($dashang)) {
                foreach ($dashang as $record) {
                    $dashang_money += $record['money'];
                }
            }

            $this->data[] = [
                'rank' => $i,
                'name' => $userid2User[$val['userid']]['nick_name'],
                'photo' => $userid2User[$val['userid']]['avatar'],
                'sex' => $userid2User[$val['userid']]['sex'] ? 'F' : 'M',
                'level' => $userid2User[$val['userid']]['level'],
                'contribution' => $val['value'],
                'note_num' => !empty($userid2NoteCnt[$val['userid']]) ? $userid2NoteCnt[$val['userid']] : 0,
                'note_reply_num' => $note_reply_cnt,
                'note_praise' => $praise_cnt,
                'reward' => $dashang_money,
            ];
            $i++;
        }

    }
}
